<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\pockets\constInterface\LinkConf;
use Doraemon\tools\ImgUrl;
use Doraemon\tools\LinkFormat;

class Tab2Channel extends BaseModule
{

    const FORMAT_HIT = 'formatPosHit';

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {

        $response = [];

        if (empty($hit)) return $response;

        $hit = array_values($hit);

        foreach ($hit as $k => $v) {
            $v['pos'] = $k + 1;
            $pic_ids = json_decode($v['pic_id'], true);
            $top = $pic_ids['big'] ?? '';
            $title = $pic_ids['title'] ?? '';
            $sub_title = $pic_ids['sub_title'] ?? '';
            $icon_id = $pic_ids['icon_id'] ?? '';
            $small_icon_id = $pic_ids['small_icon_id'] ?? '';
            $link = LinkFormat::generateLink($v['link_type'], $v['link_params']);
            $linkParams = json_decode($v['link_params'], true);

            if ($v['link_type'] == LinkConf::LINK_WHOLE_HOUSE_CHANNEL) {
                $params = [
                    'order_type' => '1',
                    'city' => [
                        'city_name' => $_REQUEST['city_name'] ?? '北京',
                        'lid' => $_REQUEST['lid'] ?? '1'
                    ],
                    'params' => [
                        [
                            'name' => '作者',
                            'type' => '4',
                            'params' => [
                                [
                                    'key' => 'user_type',
                                    'value' => '2'
                                ],
                            ]
                        ],
                    ]
                ];
                $link['app_template'] = LinkConf::LINK_APP_ENCRYPTION . base64_encode("hhz://shaijia_fiterparams:" . json_encode($params, JSON_UNESCAPED_UNICODE));
            }

            if (in_array($v['link_type'], [LinkConf::LINK_APP_FULLSCREEN, LinkConf::LINK_APP_HTTPS]) && strstr($linkParams['url'], 'designer/recommend-list')) {
                $linkArr = explode("?", $linkParams['url']);
                $url = empty($_REQUEST['lid']) ? $linkParams['url'] : $linkArr[0] . '?lid=' . $_REQUEST['lid'];
                $link = LinkFormat::generateLink($v['link_type'], json_encode(['url' => $url]));
            }
            $link = isset($link['app_template']) ? $link['app_template'] : '';
            $response[] = [
                'id' => intval($v['id']),
                'display_type' => 2,
                'top_title' => $top,
                'title' => $title,
                'sub_title' => $sub_title,
                'location_no' => intval($v['pos']),
                'pic_url' => empty($icon_id) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $icon_id, ImgUrl::IMAGE_SIZE_OP_ORI),
                'small_icon' => empty($small_icon_id) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $small_icon_id, ImgUrl::IMAGE_SIZE_OP_ORI),
                'link' => $link,
                'part_name' => $title,
                'statSign' => $this->statSign($v, $statSignExt + [\Doraemon\service\statSign\Basic::EXTEND_KEY_KEY_WORD => $title]),
            ];
        }

        if ($response) {
            $location_no = array_column($response, 'location_no');
            array_multisort($location_no, SORT_ASC, $response);
        }

        return $response;
    }

    public function formatAdmListExtInfo(&$v, $map)
    {
        $pic_ids = json_decode($v['pic_id'], true);
        $v['big'] = $pic_ids['big'] ?? '';
        $v['title'] = $pic_ids['title'] ?? '';
        $v['sub_title'] = $pic_ids['sub_title'] ?? '';
        $v['icon_url'] = empty($pic_ids['icon_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_ids['icon_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
        $v['ext_icon_url'] = empty($pic_ids['small_icon_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_ids['small_icon_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
    }

    public function getExtInfo($baseInfo)
    {
        $pic_id = json_decode($baseInfo['pic_id'], true);
        return [
            'big' => $pic_id['big'] ?? '',
            'title' => $pic_id['title'] ?? '',
            'sub_title' => $pic_id['sub_title'] ?? '',
            'icon_id' => $pic_id['icon_id'] ?? '',
            'small_icon_id' => $pic_id['small_icon_id'] ?? '',
        ];
    }

    public function uniquePos($v): string
    {
        return $v['pos'];
    }
}